<?php
/**
 * @package Admin
 * @author Henrique Droog <henridroog@gmail.com>
 * @name SubmenuController
 * @since 02/04/2011
 * @version 1.0
 */
class Admin_SubmenuController extends Zend_Controller_Action
{
    protected $_form;
    protected $_submenu;
    protected $_submenuMapper;
    protected $_request;

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        $submenu = new Admin_Model_SubmenuMapper();
        $dados = $submenu->selecionaAll();


//        $paginator = new Zend_Paginator(new Zend_Paginator_Adapter_DbSelect($dados));
//	$paginator->setItemCountPerPage(3)
//		  ->setCurrentPageNumber($this->_getParam('page', 1))
//		  ->setPageRange(3);
//        $submenus = array();
//        foreach ($paginator as $sb):
//          $submenus[] = $sb;
//        endforeach;
	$this->view->entries = $dados;
	//$this->view->paginator = $paginator;
    }

    private function getForm(){
        $form = new Admin_Form_SubmenuForm();
        $form->setAction(Zend_Controller_Front::getInstance()->getBaseUrl().'/admin/submenu/add/')
             ->setMethod('post')
             ->setAttrib('id','form')
             ->setName('form');

        return $form;
    }

    public function addAction()
    {
        $this->_form = $this->getForm();

        $request = $this->getRequest();
        if ($this->getRequest()->isPost()) {
            if ($this->_form->isValid($request->getPost())) {
                echo "<pre> Dados: ";
                    print_r($this->_form->getValues());
                echo "</pre>";
                //die();
                $submenu = new Admin_Model_Submenu($this->_form->getValues());
                $mapper = new Admin_Model_SubmenuMapper();
                $mapper->save($submenu);
                return $this->_helper->redirector('index');
            } else {
                $this->_form->populate($this->_request->getPost());
            }
        }
        $this->view->form = $this->_form;
        //$this->view->render('add.phtml');
    }

    public function editAction()
    {
        $this->_form = $this->getForm();
        $request = $this->getRequest();
        $id['id'] = $request->getParam('id');
        if ($id!='') {
           $submenu = new Admin_Model_Submenu($id);
           $mapper = new Admin_Model_SubmenuMapper();
           $res = $mapper->find($id, $submenu);
           $dataForm["id"] = $res->id;
           $dataForm["name"] = $res->name;
           $dataForm["status"] = $res->status;
           $dataForm["menu"] = $res->menu;
           $dataForm["module"] = $res->module;
           $dataForm["controller"] = $res->controller;
           $dataForm["action"] = $res->action;
           if(count($res)>=1){
               $this->_form->populate($dataForm);
           }
        }
       // die();
        $this->view->form = $this->_form;
    }

    public function deleteAction()
    {
        $request = $this->getRequest();
        $id['id'] = $request->getParam('id');
        if ($id!='') {
            $submenu = new Admin_Model_Submenu($id);
            $mapper = new Admin_Model_SubmenuMapper();
            $res = $mapper->del($submenu);
        }
        return $this->_helper->redirector('index');
    }

    public function montacombocontrollerAction(){
        $mapperController = new Admin_Model_ControllerMapper();
        $dados = $mapperController->selecionFiltros($_POST);
        $arrayOption = array();
        foreach ($dados as $d){
            $arrayOption['nome'][] = $d["controller_name"];
            $arrayOption['id'][]  = $d["controller_id"];
        }
        
        echo json_encode($arrayOption);
        die();
    }

    public function montacomboactionsAction(){
        $mapperAction = new Admin_Model_ActionMapper();
        $dados = $mapperAction->selecionFiltros($_POST);
        $arrayOption = array();
        foreach ($dados as $d){
            $arrayOption['nome'][] = $d["action_name"];
            $arrayOption['id'][]  = $d["action_id"];
        }
        
        echo json_encode($arrayOption);
        die();
    }
}